﻿using System;
using System.Collections.Generic;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using DevelopTool.Model.TipsModels;
using Sunny.UI;

namespace DevelopTool.Forms.SystemToolForms
{
    /// <summary>
    /// 关机/重启计划
    /// </summary>
    public partial class ShutdownPlanForm : BaseUIPage
    {
        /// <summary>
        /// 计时器对象
        /// </summary>
        private System.Windows.Forms.Timer timer;

        /// <summary>
        /// 倒计时：秒
        /// </summary>
        private long countdown = 0;

        public ShutdownPlanForm()
        {
            InitializeComponent();
        }

        private void ShutdownPlanForm_Load(object sender, EventArgs e)
        {
            this.Disposed += this.ShutdownPlanForm_Disposed;

            #region 绑定关机/重启类型下拉框数据
            List<dynamic> dynamics = new List<dynamic>() {
                new{ Name="关机",Type=1},
                new{ Name="重启",Type=2}
            };
            uiComboBoxShutdownType.ValueMember = "Type";
            uiComboBoxShutdownType.DisplayMember = "Name";
            uiComboBoxShutdownType.DataSource = dynamics;
            #endregion 绑定关机/重启类型下拉框数据


            #region 定时任务
            this.timer = new System.Windows.Forms.Timer
            {
                Interval = 1000,
                Enabled = false//启动计时器
            };
            this.timer.Tick += Timer_Tick;
            #endregion 定时任务
        }



        private void Timer_Tick(object sender, EventArgs e)
        {
            this.lblRemainingTime.Text = $"剩余{uiComboBoxShutdownType.SelectedText}时间：{this.countdown}秒";

            if (countdown <= 0)
            {
                switch (uiComboBoxShutdownType.SelectedValue.ToInt())
                {
                    case 1://关机
                        if (Setting.System.SendMsgModule.Contains("ShutdownPlan"))
                            FeiShuMsgHelper.SendCardMsgAsync("执行[关机]命令", "关机&重启", LogType.Warning);

                        SystemEx.RunDosCmd("shutdown -s -t 0");
                        break;
                    case 2: //重启
                        if (Setting.System.SendMsgModule.Contains("ShutdownPlan"))
                            FeiShuMsgHelper.SendCardMsgAsync("执行[重启]命令", "关机&重启", LogType.Warning);

                        SystemEx.RunDosCmd("shutdown -r -t 0");
                        break;
                }
            }

            this.countdown--;
        }


        /// <summary>
        /// 显示日志
        /// </summary>
        /// <param name="log"></param>
        private void ShowLog(LogModel log)
        {
            if (log == null) return;
            if (string.IsNullOrEmpty(log.LogStr)) return;
            string logStr = $"[{log.OperTime.String()}] {log.LogStr}{Environment.NewLine}";
            this.codeEditorLog.ContentText = this.codeEditorLog.ContentText.Insert(0, logStr);
        }


        /// <summary>
        /// 关机/重启计划开始/停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShutdownStart_Click(object sender, EventArgs e)
        {
            this.countdown = 0;
            SystemEx.RunDosCmd("shutdown -a");

            if (btnShutdownStart.Text == "开始")
            {
                //开始                             
                this.countdown = this.GetSecond(upDownShutdownH.Value, upDownShutdownM.Value, upDownShutdownS.Value);
                btnShutdownStart.Text = "停止";
                this.timer.Enabled = true;
                ShowLog(new LogModel(Model.Enums.LogType.Info, $"开始{this.lblCountdown.Text}"));
            }
            else
            {
                //停止
                btnShutdownStart.Text = "开始";
                this.timer.Enabled = false;
                this.lblRemainingTime.Text = "剩余时间：0秒";
                ShowLog(new LogModel(Model.Enums.LogType.Info, $"取消：{uiComboBoxShutdownType.SelectedText}"));
            }
        }


        /// <summary>
        /// 关机/重启计划时间选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void upDownShutdown_ValueChanged(object sender, int value)
        {
            this.lblCountdown.Text = $"倒计时{this.uiComboBoxShutdownType.SelectedText}：" + GetTime(upDownShutdownH.Value, upDownShutdownM.Value, upDownShutdownS.Value);
        }

        /// <summary>
        /// 获取总秒数
        /// </summary>
        /// <param name="hour"></param>
        /// <param name="minute"></param>
        /// <param name="second"></param>
        /// <returns></returns>
        private long GetSecond(int hour, int minute, int second)
        {
            long secondNum = 0;
            if (hour > 0)
            {
                secondNum += hour * 60 * 60;
            }
            if (minute > 0)
            {
                secondNum += minute * 60;
            }
            if (second > 0)
            {
                secondNum += second;
            }
            return secondNum;
        }


        /// <summary>
        /// 获取友好显示时间
        /// </summary>
        /// <param name="hour"></param>
        /// <param name="minute"></param>
        /// <param name="second"></param>
        /// <returns></returns>
        private string GetTime(int hour, int minute, int second)
        {
            string h = hour + "";
            if (h.Length < 2)
            {
                h = "0" + h;
            }
            string m = minute + "";
            if (m.Length < 2)
            {
                m = "0" + m;
            }
            string s = second + "";
            if (s.Length < 2)
            {
                s = "0" + s;
            }
            return string.Format("{0}时{1}分{2}秒", h, m, s);
        }

        /// <summary>
        /// 关机/重启类型选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiComboBoxShutdownType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (uiComboBoxShutdownType.ContainsFocus)
            {
                upDownShutdown_ValueChanged(sender, 0);
            }
        }


        private void ShutdownPlanForm_Disposed(object sender,EventArgs e)
        {
            if(this.timer != null)
            {
                this.timer.Stop();
                this.timer.Dispose();
            }
        }

    }
}
